package org.ym.tools;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 字符串通用操作合集
 *
 * @author ym
 * @date 2022-06-06 15:18:13
 */
public class StringTool {

    public static final String PERCENT_SYMBOL = "%";

    private StringTool() {
    }

    public static final String REG_SPLIT_COMMON_STR = ",|;|，|；";
    public static final String REG_SPLIT_COMMA = ",";

    /**
     * 字符串转数组,",|;|，|；"切割
     *
     * @param str 字符串
     * @return 数组
     */
    public static String[] splitToArr(String str) {
        return splitToArr(str, REG_SPLIT_COMMON_STR);
    }

    /**
     * 字符串转数组,",|;|，|；"切割
     *
     * @param str 字符串
     * @param reg 正则
     * @return 数组
     */
    public static String[] splitToArr(String str, String reg) {
        String[] arr;
        if (StringUtils.isNotBlank(str)) {
            arr = str.split(reg);
        } else {
            arr = new String[0];
        }
        return arr;
    }

    /**
     * 字符串转list集合,",|;|，|；"切割
     *
     * @param str 字符串
     * @return list集合
     */
    public static List<String> splitToList(String str) {
        return splitToList(str, REG_SPLIT_COMMON_STR);
    }

    /**
     * 字符串转list集合
     *
     * @param str 字符串
     * @param reg 正则
     * @return list集合
     */
    public static List<String> splitToList(String str, String reg) {
        List<String> list = new ArrayList<>();
        if (StringUtils.isNotBlank(str)) {
            list.addAll(Arrays.asList(str.split(reg)));
        }
        return list;
    }

    /**
     * 数组转字符串，“,"分隔
     *
     * @param arr 数组
     * @return 字符串
     */
    public static String join(String[] arr) {
        return join(arr, REG_SPLIT_COMMA);
    }

    /**
     * 数组转字符串，“,"分隔
     *
     * @param arr       数组
     * @param delimiter 分隔符
     * @return 字符串
     */
    public static String join(String[] arr, String delimiter) {
        String str = "";
        if (arr != null && arr.length > 0) {
            str = String.join(delimiter, arr);
        }
        return str;
    }

    /**
     * list转字符串，“,"分隔,移除 空白字符以及null字符
     *
     * @param list 集合
     * @return 字符串
     */
    public static String join(Collection<String> list) {
        return join(list, REG_SPLIT_COMMA);
    }

    /**
     * list转字符串，“,"分隔,移除 空白字符以及null字符
     *
     * @param list      集合
     * @param delimiter 分隔符
     * @return 字符串
     */
    public static String join(Collection<String> list, String delimiter) {
        String str = "";
        if (list != null && !list.isEmpty()) {
            list.removeIf(StringUtils::isBlank);
            if (delimiter == null) {
                delimiter = REG_SPLIT_COMMA;
            }
            str = String.join(delimiter, list);
        }
        return str;
    }

    /**
     * 结合字符串
     *
     * @param strArr 数据
     * @return 字符串
     */
    public static String concatStr(String... strArr) {
        StringBuilder stringBuilder = new StringBuilder();
        if (strArr != null && strArr.length > 0) {
            for (String str : strArr) {
                stringBuilder.append(objectToString(str));
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 对象转string
     *
     * @param o 对象
     * @return String
     */
    public static String objectToString(Object o) {
        return o == null ? "" : o.toString();
    }

    /**
     * 实现like
     * @param o 数据
     * @return 增加%
     */
    public static String like(Object o) {
        String value = objectToString(o);
        if (value.startsWith(PERCENT_SYMBOL)) {
            if (value.endsWith(PERCENT_SYMBOL)) {
                return value;
            } else {
                return concatStr(value, PERCENT_SYMBOL);
            }
        } else {
            if (value.endsWith(PERCENT_SYMBOL)) {
                return concatStr(PERCENT_SYMBOL, value);
            } else {
                return concatStr(PERCENT_SYMBOL, value, PERCENT_SYMBOL);
            }
        }
    }
    /**
     * 实现like
     * @param o 数据
     * @return 增加%
     */
    public static String leftLike(Object o) {
        String value = objectToString(o);
        if (value.startsWith(PERCENT_SYMBOL)) {
            return value;
        }else {
            return concatStr(PERCENT_SYMBOL,value);
        }
    }
    /**
     * 实现like
     * @param o 数据
     * @return 增加%
     */
    public static String rightLike(Object o) {
        String value = objectToString(o);
        if (value.endsWith(PERCENT_SYMBOL)) {
            return value;
        }else {
            return concatStr(value,PERCENT_SYMBOL);
        }
    }

    /**
     * 下划线转驼峰
     * user_name  ---->  userName
     * userName   --->  userName
     * User_Name ----> userName
     * _User_Name -----> userName
     * @param underlineStr 带有下划线的字符串
     * @return 驼峰字符串
     */
    public static String toCamelCase(String underlineStr) {
        if (StringUtils.isBlank(underlineStr)) {
            return underlineStr;
        }
        underlineStr = StringUtils.strip(underlineStr,"_");
        // 分成数组
        char[] charArray = underlineStr.toCharArray();
        // 判断上次循环的字符是否是"_"
        boolean isFindUnderline = false;
        StringBuilder stringBuilder = new StringBuilder();
        char charZ;
        for (int i = 0, l = charArray.length; i < l; i++) {
            charZ = charArray[i];
            if(i == 0 && isUpperCaseChar(charZ)){
                stringBuilder.append(charArray[i] += 32);
            }
            // 判断当前字符是否是"_",如果跳出本次循环
            else if (charZ == 95) {
                isFindUnderline = true;
            } else{
                if (isFindUnderline) {
                    // 如果为true，代表上次的字符是"_",当前字符需要转成大写
                    if(isLowerCaseChar(charZ)){
                        charZ -= 32;
                    }
                    isFindUnderline = false;
                }
                stringBuilder.append(charZ);
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 判断是大写字母
     * @param charZ 字符
     * @return true/false
     */
    public static boolean isUpperCaseChar(char charZ){
        return charZ >= 65 && charZ <= 90;
    }

    /**
     * 判断是大写字母
     * @param charZ 字符
     * @return true/false
     */
    public static boolean isLowerCaseChar(char charZ){
        return charZ >= 97 && charZ <= 122;
    }

    /**
     * 驼峰转 下划线
     * userName  ---->  user_name
     * user_name  ---->  user_name
     * UserName ----->  user_name
     * @param camelCaseStr 驼峰字符串
     * @return 带下滑线的String
     */
    public static String toUnderlineCase(String camelCaseStr) {
        if (StringUtils.isBlank(camelCaseStr)) {
            return camelCaseStr;
        }
        // 将驼峰字符串转换成数组
        char[] charArray = camelCaseStr.toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        char charZ;
        //处理字符串
        for (int i = 0, l = charArray.length; i < l; i++) {
            charZ = charArray[i];
            if (isUpperCaseChar(charZ)) {
                if(i != 0){
                    stringBuilder.append("_");
                }
                charZ += 32;
            }
            stringBuilder.append(charZ);
        }
        return stringBuilder.toString();
    }
}
